<?php
/**
 * Paths setup. Creates global link constants
 *
 * @copyright (c) 2006 A.Sleep <a.sleep@asleep.net> under the GNU GPL version 2
 * @author A.Sleep <a.sleep@asleep.net>
 */

preg_match("/(.*\/)[^\/]+/", $_SERVER['SCRIPT_NAME'], $matches);

$path = $matches[1];

$relative_root = './';
$rrCount = 0;

while (!file_exists($relative_root.'.web_root') and $rrCount < 5) {
  $rrCount ++;
  $relative_root .= '../';
}

while ($rrCount > 0) {
  if (!preg_match('/(.*\/)[^\/]+\//', $path, $matches)) {
    /* fatal error */
    // @todo Do something here
    echo 'Root Error';
    exit;
  }

  $path = $matches[1];
  $rrCount --;
}

if (!isset($path)) {
  $path = '/';
}

/**
 * @global LINK_ROOT
 * @var string Web-root for links (URL, not local)
 */
define('LINK_ROOT', $path);

/**
 * @global SECURE_LINK_ROOT
 * @var string Secure (SSL) version of LINK_ROOT
 * @see LINK_ROOT
 */
define('SECURE_LINK_ROOT', 'https://'.$_SERVER['HTTP_HOST'].$path);

/**
 * @global RELATIVE_ROOT
 * @var string Filesystem relative root to current doc
 */
define('RELATIVE_ROOT', $relative_root);

/**
 * @global APP_DIR
 * @var string Filesystem path (using relative root) to the Apps directory
 */
define('APP_DIR', $relative_root.'Apps/');

/**
 * @global IMAGE_ROOT
 * @var string Image root i.e. images.SITE.TLD
 */
define('IMAGE_ROOT', LINK_ROOT);

/**
 * @global IMAGE_PATH
 * @var string Image path local (eg. disk path, not url)
 */
define('IMAGE_PATH', RELATIVE_ROOT.'images/');

/**
 * @global HOST_NAME
 * @var string Current Domain w/o the dev and stage bits
 */
$hostName = str_replace(array('dev.', 'stage.'), '', $_SERVER['HTTP_HOST']);
define('HOST_NAME', $hostName);
?>
